<?php

namespace app\admin\controller\daily;
use think\Db;
/**
 * 
 * 订单
 *
 * @icon fa fa-circle-o
 */
class Orderoff
{
     /**
     * 
     * 未支付订单15分钟内自动取消
     *
     * @icon fa fa-circle-o
     */
    public function index()
    {
        $where['ontime'] = ['lt',time()-15*60];
        $where['status'] = 1;
        $list = DB::name('order')->where($where)->field('goods_id,id,num,buy_type')->select();
        // dump($list);exit;
        if($list){
            foreach($list as $k=>$v){
                $data['status'] = 9;
                $data['reason'] = '系统自动取消';
                DB::name('order')->where(['id'=>$v['id']])->update($data);
                Db::name('goods')->where('id',$v['goods_id'])->setInc('stock',$v['num']);
                if ($v['buy_type'] == 3) {
                	$ck['status'] = 0;
                	$ck['order_id'] = 0;
                	Db::name('goodsinfo')->where('order_id',$v['id'])->update($ck);
                }
            }
        }
    }


    /**
     *
     * 未支付收货商城订单15分钟内自动取消
     *
     * @icon fa fa-circle-o
     */
    public function home()
    {
        $where['ontime'] = ['lt',time()-15*60];
        $where['status'] = 1;
        $where['order_user'] = 1;
        $list = DB::name('reorder')->where($where)->field('goods_id,id,num')->select();
        // dump($list);exit;
        if($list){
            /*foreach($list as $k=>$v){
                $data['status'] = 9;
                DB::name('order')->where(['id'=>$v['id']])->update($data);
                Db::name('goods')->where('id',$v['goods_id'])->setInc('stock',$v['num']);
            }*/
            foreach($list as $k=>$v){
                $data['status'] = 4;
                $data['reason'] = '系统自动取消';
                DB::name('reorder')->where(['id'=>$v['id']])->update($data);
                Db::name('receiving')->where('id',$v['goods_id'])->setInc('num',$v['num']);
            }
        }
    }

     /**
     * 
     * 金币已发货订单一个小时自动确认收货
     *
     * @icon fa fa-circle-o
     */
    public function orderok()
    {
        //查询已发货一个小时未确认收货的订单
        $where['status'] = 3;
        $where['sendtime'] = ['lt',time()-60*30];
        $list = DB::name('order')
                ->where($where)->field('buyid,amount,uid,xyg,status,id,goods_id')->select();
        if($list){
            foreach($list as $k=>$v){
                if($v['status'] == 3){
                    $goods = DB::name('goods')->where(['id'=>$v['goods_id']])->field('goods_class_type,is_safe,method')->find();
                    //已完成的订单给卖家到账
                    $money = $this->getAmount($v['amount'], $goods['goods_class_type'], $goods['is_safe'], $v['xyg'], $goods['method']);
                    $res = $this->addAccountLog($v['uid'], '+'.$money, 2, '售得', 1,'');
                    // dump($res);exit;
                    if ($res !== false) {
                        $uinfo = Db::name('personal')->where(['id'=>$v['uid']])->field('balance,jy_balance')->find();
                        //用户余额增加
                        Db::name('personal')->where(['id'=>$v['uid']])->setInc( 'balance', $money );
                        $uinfo_after = Db::name('personal')->where(['id'=>$v['uid']])->field('balance,jy_balance')->find();
                        // dump($uinfo);
                        $this->addFinanceLog($v['uid'],'售得',$uinfo['balance'],$uinfo['jy_balance'], $money, $uinfo_after['balance'],$uinfo_after['jy_balance'], '发货30分钟未收货自动确认收货');
                    }
                    //更新订单状态和完成时间
                    $update['overtime'] = time();
                    $update['status'] = 5;
                    Db::name('order')->where('id',$v['id'])->update($update);
                }

            }
        }
    }



    /**
     *
     * 收获商城已发货订单一个小时自动确认收货
     *
     * @icon fa fa-circle-o
     */
    public function reorderok()
    {
        //查询已发货一个小时未确认收货的订单
        $where['status'] = 3;
        $where['sendtime'] = ['lt',time()-60*3];
        $list = DB::name('reorder')
            ->where($where)->field('buyid,order_user,prices,price,num,uid,status,id,goods_id')->select();
        if($list){
            foreach($list as $k=>$v){
                if($v['status'] == 3){
                    if ($v['order_user'] == 1) {
                        $money = $v['price'] * $v['num'] * 0.98;
                    } else {
                        $money = $v['price'] * $v['num'] * 0.9952;
                    }

                    $res = $this->addAccountLog($v['uid'], '+'.$money, 2, '售得', 1,'');
                    // dump($res);exit;
                    if ($res !== false) {
                        $uinfo = Db::name('personal')->where(['id'=>$v['uid']])->field('balance,jy_balance')->find();
                        //用户余额增加
                        Db::name('personal')->where(['id'=>$v['uid']])->setInc( 'balance', $money );
                        $uinfo_after = Db::name('personal')->where(['id'=>$v['uid']])->field('balance,jy_balance')->find();
                        // dump($uinfo);
                        $this->addFinanceLog($v['uid'],'售得',$uinfo['balance'],$uinfo['jy_balance'], $money, $uinfo_after['balance'],$uinfo_after['jy_balance'], '发货30分钟未收货自动确认收货');
                    }
                    //更新订单状态和完成时间
                    $update['overtime'] = time();
                    $update['status'] = 5;
                    Db::name('reorder')->where('id',$v['id'])->update($update);
                }

            }
        }
    }


    /**
    * 添加财务记录
    */
    public function addFinanceLog($uid,$title,$before_balance,$before_jybalance, $money, $balance,$jy_balance, $content)
    {
        $data = [
            'uid'               => $uid,
            'title'             => $title,
            'before_balance'    => $before_balance,
            'before_jybalance'  => $before_jybalance,
            'money'             => $money,
            'balance'           => $balance,
            'jy_balance'        => $jy_balance,
            'content'           => $content,
            'createtime'        => time(),
            'admin_id'          => 0,
            'admin'             => '定时任务',
        ];
        $result = Db::name('finance_log')->insert($data);
        return $result;
    }


    /**
     * 添加日志明细
     */
    public function addAccountLog($uid, $money, $type, $content, $status, $arrivaltime,$form=1)
    {
        $data = [
            'uid'       => $uid,
            'log_sn'    => date('YmdHis') . rand(10000000,99999999),
            'money'     => $money,
            'type'      => $type,
            'content'   => $content,
            'status'    => $status,
            'createtime'=> time(),
            'arrivaltime'=> $arrivaltime?:time(),
            'form'       => $form,
        ];
        $result = DB::name('account_log')->insert($data);
        return $result;
    }

    public function getAmount($amount, $type, $safe, $xyg, $method)
    {
        $map['type'] = $type;
        $map['safe'] = $safe;
        $map['xyg'] = $xyg;
        $map['method'] = $method;
        $fee = Db::name('server')->where($map)->find();
        $trade = $amount * $fee['trade'];
        if($trade < 1){
            $amount = $amount - 1;
        }else{
            $amount = $amount * (1-$fee['trade']);
        }
        return round($amount,2);
    }
}
